package com.mrjy.wechat.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import java.util.Properties;
/**
 * 微信配置文件读取
 * @author csshui
 * date : 2017-04-24 AM
 *
 */
public class WxConfig implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6753226605745581232L;
	
	private static Properties props = null;
	private static File configFile = null;

	private static long fileLastModified = 0L;
	
	/*
	 *  初始化
	 */
	private static void init() {
		URL url = WxConfig.class.getClassLoader().getResource("wxconfig.properties");
		configFile = new File(url.getFile());
		fileLastModified = configFile.lastModified();
		props = new Properties();
		load();
	}
	
	/*
	 * 加载配置文件
	 */
	private static void load() {
		try {
			props.load(new FileInputStream(configFile));
			fileLastModified = configFile.lastModified();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static String getProperty(String key) {
		if ((configFile == null) || (props == null))
			init();
		if (configFile.lastModified() > fileLastModified)
			load();
		System.out.println("-----------------------------key:"+key);
		return props.getProperty(key);

	}

}
